package com.xuetang9.t9.hotel.mapper;


import com.xuetang9.t9.hotel.beans.Guest;
import com.xuetang9.t9.hotel.mapper.condition.ICondition;
import com.xuetang9.t9.hotel.mapper.sql.impl.GuestMapperSql;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author Pang
 */
@Repository
public interface IGuestMapper {

    /**
     * 新增客人信息
     * @param guest
     * @return
     */
    @Insert("INSERT INTO g_guest(guest_id,guest_name,card_type,card_num,guest_phone,card_addr,guest_sex,guest_state) " +
            "VALUES(#{guestId},#{guestName},#{cardType},#{cardNum},#{guestPhone},#{cardAddr},#{guestSex},#{guestState})")
    int insert(Guest guest);

    /**
     * 更新客人信息
     * @param guest
     * @return
     */
    @Update("UPDATE g_guest SET guest_name=#{guestName},card_type=#{cardType},card_num=#{cardNum},guest_phone=#{guestPhone}," +
            "card_addr=#{cardAddr},guest_sex=#{guestSex},guest_state=#{guestState} WHERE guest_id=#{guestId}")
    int update(Guest guest);


    /**
     * 删除客人信息
     * @param id
     * @return
     */
    @Delete("DELETE FROM g_guest WHERE guest_id=#{guestId}")
    int delete(Long id);


    /**
     * 更加查询条件查找
     * @param guestCondition
     * @return
     */
    @SelectProvider(value = GuestMapperSql.class, method = "selectByAll")
    @Results(value = {
            @Result(id = true,column = "guest_id",property = "guestId"),
            @Result(column = "guest_id",property = "arrives",many = @Many(select = "com.xuetang9.t9.hotel.mapper.IArriveMapper.selectByGuestId",fetchType = FetchType.EAGER))
    })
    List<Guest> selectByAll(@Param("key") ICondition guestCondition);


    /**
     * 根据条件查找总数量
     * @param guestCondition
     * @return
     */
    @SelectProvider(value = GuestMapperSql.class, method = "selectByCount")
    Long selectByCount(@Param("key") ICondition guestCondition);

    /**
     * 根据Id查询客人信息
     * @param id
     * @return
     */
    @Select("SELECT * FROM g_guest WHERE guest_id=#{guestId}")
    Guest selectById(Long id);

    /**
     * 根据证件号精确查询
     * @param num
     * @return
     */
    @Select("SELECT * FROM g_guest WHERE card_num = #{id}")
    Guest selectByNum(@Param("id") String num);

}
